<?php
namespace app\index\controller;

use app\index\Controller;


class Index extends Base
{   

    public function index(){
        echo "^_^";
        echo "<br/>";
        echo  date('Y-m-d H:i',time());
    }

    public  function __construct(){
        parent:: __construct();
    }

    public function getVerifity(){
        $User = model('User');
        
        $mobile   = input('mobile'); 
        $verify   = rand(1000,9999);
        
        $data['status'] = 0;
        if($User->getVerifity($mobile,$verify)){
            $data['status']   = 1;
            $data['verify'] = $verify;
        }
        return json($data);
    }

    //绑定手机号
    public function bindMobile(){
        $Redpacket = model('redpacket');
    
        $update_info['mobile']              = input('mobile');
        $update_info['mobile_binding_time'] = time();
    
        $data['status'] = 0;

        if(db('user')->where('id',$this->user_id)->update($update_info)){

            //为用户发放红包
            $Redpacket->giveUserRedpacket($this->user_id,1,4.9);
            $data['status'] = 1;
            $data['info']   = '绑定成功！';
        }

        return json($data);
    }

    //保存用户的微信基本信息
    public function addUserInfo(){
        $user_info  = json_decode(input('rawData'),true);
        $id         = $this->user_id;
       
        if(is_array($user_info) && $id){
            $wx_user_info['nick_name']  = $user_info['nickName'];
            $wx_user_info['avatar_url'] = $user_info['avatarUrl'];
            $update_result = db('user')->where('id',$id)->update($wx_user_info);

        }
    }
    
    //验证用户是否已经缴纳押金
    public function checkDeposit(){
        $deposit = db('user')->where('id',$this->user_id)->value('deposit');
        $result  = db('system_config')->where('id',1)->find();
        
        $where['user_id'] = $this->user_id;
        $where['status']  = 0;

        if($redpacket_info = db('user_redpacket')->where($where)->field('id as redpacket_id,max(price) as redpacket_price')->find()){
        	$data['redpacket_info'] = $redpacket_info;
        }
    	
        $data['redpacket_info']['redpacket_id']    = $data['redpacket_info']['redpacket_id']       ? $data['redpacket_info']['redpacket_id']    : 0;
    	$data['redpacket_info']['redpacket_price'] = $data['redpacket_info']['redpacket_price'] ? $data['redpacket_info']['redpacket_price'] : 0;
        

        $data['deposit'] = 0;
        $data['amount']  = $result['amount'];
        if($deposit == 0){
           $data['deposit'] = $result['deposit'];
        }
        return json($data);
    }

    //获取用户缴纳的押金
    public function getUserDeposit(){
        $data['deposit'] = db('user')->where('id',$this->user_id)->value('deposit');
        return json($data);
    }

    //提交订单
    public function submitOrder(){
        $Lock   = model('Lock');//实例化锁模型
        $Charge = model('Charge');//实例化消费记录模型
        $number       = input('ball_arm_number');
        $redpacket_id = input('redpacket_id');
        $amount       = input('amount');
 
        $redpacket_price = db('user_redpacket')->where('id',$redpacket_id)->value('price');
       
         //根据球杆编号查询该球杆的状态信息
        $lock_info = $Lock->getLockInfo($number);
        

        $order_info['price']      = $amount;
        $order_info['user_id']    = $this->user_id;
        $order_info['form_id']    = input('form_id');
        $order_info['user_redpacket_id'] = $redpacket_id;
        $order_info['amount']     = $amount - $redpacket_price;
        $order_info['deposit']    = input('deposit');

        $order_info['lock_id']    = $lock_info['id']; 

        if($data['charge_id'] = $Charge->addOrder($order_info)){
            $data['status'] = 1;
            $data['amount'] = $order_info['amount'];
        }
        
        return json($data);
    }
    
    //支付订单
    public function payOrder(){
        $Charge  = model('charge');
        
        $charge_id   = input('charge_id');
        $charge_info = db('charge_log')->where('id',$charge_id)->field('amount,deposit,out_trade_no')->find();
        
        $amount = ($charge_info['amount']+$charge_info['deposit'])*100;
        $data['result'] = $Charge->pay_place_order($this->user_id,$amount,$charge_info['out_trade_no']);     
        
        return json($data);
    }

    //获取用户总共使用时间与红包剩余个数
    public function getUseTotalTime(){
        $where['user_id'] = $this->user_id;
        $where['is_pay']  = 1;
        $data['use_total_time']  = db('charge_log')->where($where)->value('sum(duration)');
        $data['use_total_time'] = ceil($data['use_total_time']/60);
        unset($where['is_pay']);
        $where['status'] = 0;
        $data['redpacket_count'] = db('user_redpacket')->where($where)->count();

        return json($data);
    }


    //用户取杆即开锁操作
    public function unlock()
    {   
        $Lock   = model('Lock');//实例化锁模型
        $Charge = model('Charge');//实例化消费记录模型
        
        //获取用户扫描二维码的标号
        $number = input('ball_arm_number');

        //根据球杆编号查询该球杆的状态信息
        $lock_info = $Lock->getLockInfo($number);
        //获取球房的编号
        $club_id                  = db('box')->where('id',$lock_info['box_id'])->value('club_id');
        $lock_info['club_number'] = db('club')->where('id',$club_id)->value('club_number');
        
        $data['status'] = 0;
        
        //用户取球杆的业务处理
        if($lock_info['status'] == 0){
            //通知远程柜子开锁
            if($this->request($lock_info['control_board_number'],$lock_info['lock_number'],1)){
                //修改杆的状态为使用中
                $data['status'] = $Lock->updateLockUsingStatus($lock_info['id'],$this->user_id);
            
                $data['lock_info'] = $lock_info;
            }else{
                $data['info'] = "通讯失败！";
            }
        }else{
            //该球杆正在被用户使用，提示用户使用其它球杆
            $data['info']   = '该球杆正在被使用中';
        }

        return json($data);
    }

    //验证该球杆使用状态
    public function checkBarUseState(){
        $number    = input('ball_arm_number');

        $data['status'] = 0;

        if(db('lock')->where('number',$number)->value('status')){
            $data['status'] = 1;
            $data['info']   = '该球杆正在被使用中';  
        }
        return json($data);
    }
    
    //用户还杆
    public function returnBallArm(){
        $Lock   = model('Lock');//实例化锁模型
       
        $number    = input('ball_arm_number');
        
        $return_info  = db('lock')->where('number',$number)->field('user_id,is_pass')->find();
        
        $is_pass = $return_info['is_pass'];
        if($return_info['user_id'] != $this->user_id){
            $result['status'] = 1;
        }else if($is_pass == -1){
            $result['status']     = -1;
            $result['error_info'] = '球杆还的锁编号不对！';
        }else if($is_pass == 2){
            $result['status']     = 2;
            $result['error_info'] = '通讯错误！请稍后重试';
        }else{
            $result['status']     = 0;
            $result['error_info'] = '未检测到球杆';
        }

        return json($result);
    }

    //用户还杆
    public function giveBack(){
        $Charge       = model('Charge');//实例化锁模型
        $charge_id    = input('charge_id');
        

        $data['status'] = 0;//未结算
        $data['into']   = '未结算';
        if($Charge->checkChargeISEnd($charge_id)){
            $data['status'] = 1;
            $data['info']   = '已结算';
        }
       
        return json($data);
    }

    // //是否通过用户还杆的操作
    // public function isPassReturnBallArm(){
    //     $number = input('number');
    //     $update['is_pass'] = input('state');
    //     $update['is_pass'] = $update['is_pass'] == 0 ? '-1' : $update['is_pass'];
    //     $result = db('lock')->where('number',$number)->update($update);
    //     if($result || $result === 0){
    //         $data['state'] = 1;
    //         $data['info'] = '通讯成功！';
    //     }else{
    //         $data['state'] = 0;
    //         $data['info']  = '通讯失败';
    //     }
    //     return json($data);
    // }

 

    //开锁的接口
    private function request($equipment,$code,$on){
        $data['equipment'] = $equipment;
        $data['code']      = $code;
        $data['on']        = $on;
  
        $url = "http://47.93.103.53:8080/WebService1.asmx/MQTTService";
        // $url    = "http://47.93/WebService1.asmx/MQTTService";
        $result = httpRequest($url,'POST',$data);
      
        return $result;
    }

    //用户的消费使用记录
    public function usagelog(){
        $Charge        = model('charge');
        $user_charge = $Charge->getUserCharge($this->user_id);
        
        $user_charge_list = array();
        if(is_array($user_charge)){
            foreach ($user_charge as $key => $value) {
                $user_charge_list[$key][] = date('Y-m-d H:i',$value['end_time']);
                $user_charge_list[$key][] = '力杆-野豹';
                $user_charge_list[$key][] = formatDuration($value['duration']);
                $user_charge_list[$key][] = $value['club_name'];
                $user_charge_list[$key][] = $value['amount'];
            }
            return json($user_charge_list);
        }
    }

    //用户的红包
    public function userRedpacket(){
        $Redpacket = model('redpacket');
        //用户红包list
        $data['user_redpacket_list'] = $Redpacket->getUserRedpacketList($this->user_id);
        foreach ($data['user_redpacket_list'] as $key => $val) {
            $data['user_redpacket_list'][$key]['use_time'] = $data['user_redpacket_list'][$key]['use_time'] == 0 ? 0 : date('Y-m-d H:i',$data['user_redpacket_list'][$key]['use_time']);
        }
        //用户未使用红包数量

        return json($data);
    }

    //用户使用红包
    public function useRedPacket(){
        $Redpacket = model('redpacket');
        $Recharge  = model('recharge');

        $id = input('id');
        
        $data['status'] = 0;
        
        //使用红包
        $use_result = $Redpacket->userUseRedPacket($id);
        if($use_result >=0){
            //账户充值
            if($money = $Recharge->userRecharge($this->user_id,$use_result)){
                //添加充值记录
                if($Recharge->addRechargeLog($this->user_id,$use_result,2)){
                    $data['money']  = $money;
                    $data['status'] = 1;
                }

            }
            
        }
        return json($data);
    }


    //获取用的的money
    public function getMoney(){
        return db('user')->where('id',$this->user_id)->value('money');
    }

    //用户反馈
    public function addUserFeedback(){
        $Lock = model('Lock');
        $ball_arm_number = input('ball_arm_number');
        if($ball_arm_number){
           //根据球杆编号查询该球杆的状态信息
            $lock_info = $Lock->getLockInfo($ball_arm_number);
            $insert_info['lock_id'] = $lock_info['id'];
        }
        $insert_info['user_id']          = $this->user_id;
        $insert_info['feedback_type']    = input('feedback_type');
        $insert_info['feedback_content'] = input('feedback_content');
        $insert_info['time']             = time();


        $data['status'] = 0;
        if(db('user_feedback')->insert($insert_info)){
            $data['status'] = 1;
        }
        return json($data);

     }

   
}
